package com.swgk.core.common;

import android.widget.ImageView;
import androidx.databinding.BindingAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.swgk.core.R;
import com.swgk.core.util.DisplayUtil;


public class ImageViewAdapter {

    @BindingAdapter("imageUrl")
    public static void bindImageUrl(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.drawable.null_img_list)
                .fallback( R.drawable.null_img_list)
                .error(R.drawable.null_img_list)).into(view);
    }
    @BindingAdapter("imageUrlRound")
    public static void bindImageUrlRound(ImageView view, String imageUrl){
        RoundedCorners roundedCorners= new RoundedCorners(4);
//通过RequestOptions扩展功能,override采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = new RequestOptions()
                .bitmapTransform(roundedCorners).override(300, 300)
                .placeholder(R.drawable.null_img_list)//图片加载出来前，显示的图片
                .error(R.drawable.null_img_list);//图片加载失败后，显示的图片
        Glide.with(view.getContext())
                .load(imageUrl)
                .apply(options)//异常时候显示的图片
                .into(view);
    }

    @BindingAdapter("imageUrlForShop")
    public static void setImageUrl(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.shop_img)
                .fallback( R.mipmap.shop_img)
                .error(R.mipmap.shop_img)).into(view);
    }

    @BindingAdapter("imageUrlForCompany")
    public static void imageUrl(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.gongsiqiye)
                .fallback( R.mipmap.gongsiqiye)
                .error(R.mipmap.gongsiqiye)).into(view);
    }

    @BindingAdapter("imageUrlForTeam")
    public static void teamImageUrl(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.morentu_change)
                .fallback( R.mipmap.morentu_change)
                .error(R.mipmap.morentu_change))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(2)))
                .into(view);
    }

    @BindingAdapter("imageUrlForTeam_lib")
    public static void teamImageUrl_lib(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.morentu_change)
                .fallback( R.mipmap.morentu_change)
                .error(R.mipmap.morentu_change))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(2)))
                .into(view);
    }

    @BindingAdapter("imageUrlForTeamdetail")
    public static void teamDetailImageUrl(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.wulogo_team_heand)
                .fallback( R.mipmap.wulogo_team_heand)
                .error(R.mipmap.wulogo_team_heand))
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(view);
    }


    @BindingAdapter("imageUrlForMapEnterprise")
    public static void mapEnterpriseImageUrl(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.enterprise_map)
                .fallback( R.mipmap.enterprise_map)
                .error(R.mipmap.enterprise_map))
                .into(view);
    }

    @BindingAdapter("imageUrlForManage")
    public static void mapManageImageUrl(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.shop_img_new_2)
                .fallback( R.mipmap.shop_img_new_2)
                .error(R.mipmap.shop_img_new_2))
                .into(view);
    }

    @BindingAdapter("imageUrlForManageVideo")
    public static void mapManageImageVideo(ImageView view, String imageUrl){
        Glide.with(view.getContext()).load(imageUrl).apply(new RequestOptions()
                .placeholder(R.mipmap.wulogo_manage)
                .fallback( R.mipmap.wulogo_manage)
                .error(R.mipmap.wulogo_manage))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(DisplayUtil.dip2px(view.getContext(),2))))
                .into(view);
    }
}
